package com.sto.stoapp.db.manager;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import com.sto.stoapp.db.StoDataBase;
import com.sto.stoapp.db.StoTableFieldName;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.status.EnumTableNames;
import com.sto.stoapp.status.OperateEnumTable;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.util.UnifiedStaticType;

/**
 * 删除数据
 * 
 * @author wxf
 * 
 */
public class DeleteDataManager extends StoDataBase {
	private Context context;
	private static DeleteDataManager deleteDataManager;

	List<String> listCode = new ArrayList<String>();

	public DeleteDataManager(Context context) {
		super(context);
		this.context = context;
	}

	public static DeleteDataManager getInstance(Context context) {
		if (deleteDataManager == null) {
			deleteDataManager = new DeleteDataManager(context);
		}
		return deleteDataManager;

	}

	/**
	 * 删除记录数据
	 * 
	 * @author wxf
	 * 
	 */
	public void deleteRecordData() {
		SimpleDateFormat sDateFormat = new SimpleDateFormat(
				"yyyy/MM/dd HH:mm:ss");
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(new Date());
		gc.add(2, -1);

		String delDate = sDateFormat.format(gc.getTime()); // 取上一个月的日期
		StoTableFieldName tableFieldName = new StoTableFieldName();

		String sql = "";

		for (EnumTableNames item : OperateEnumTable.getAllTables()) {

			String tableName = item.getTableName();

			sql = "delete from " + tableName + " where "
					+ tableFieldName.SEND_STATE + " = ? and "
					+ tableFieldName.UPLOAD_TIME + " < ?";

			synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
				SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE);
				try {
					db.beginTransaction();

					db.execSQL(sql, new Object[] {
							UnifiedStaticType.YES_STATUS, delDate });

					db.setTransactionSuccessful();

				} catch (Exception e) {
					e.printStackTrace();
					ExceptionLog.writeLog("deleteRecordData", e.getMessage());
				} finally {

					db.endTransaction();
					closeDb(UnifiedStaticType.WRITE_DATABASE);

				}
			}
		}

		delPhoto(gc.getTime());

	}

	private void delPhoto(final Date date) {
		SimpleDateFormat sDateFormat = new SimpleDateFormat(
				UnifiedStaticType.SIMPLE_DATE_FORMAT);

		// String monthNow = sDateFormat.format(new Date());

		String monthNow = sDateFormat.format(date);

		delFile(UnifiedStaticType.QS_IMAGE_PATH, monthNow);

		delFile(UnifiedStaticType.WTJ_IMAGE_PATH, monthNow);

	}

	private void delFile(final String dir, final String monthNow) {
		// 年月日目录
		if (Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())) {
			File aDirectory = new File(dir);
			if (aDirectory.exists()) {
				for (File f : aDirectory.listFiles()) {
					if (f.isDirectory()) {
						if (Helper.isNumeric(f.getName())) {
							if (Integer.parseInt(f.getName()) < Integer
									.parseInt(monthNow)) {
								// 要删除的目录
								File[] childFiles = f.listFiles();
								if (childFiles == null
										|| childFiles.length == 0) {
									f.delete();

									return;
								}
								for (File file : childFiles) {
									file.delete();
								}
								f.delete();
							}
						}
					}
				}
			}
		}
	}

}
